<template>
  <!-- 评价组件 -->
  <div class="goods-comment" v-if="commentInfo">
    <!-- 评价头部 -->
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            v-for="(item, index) in commentInfo.tags"
            :key="item.title"
            @click="changeTag(index)"
            :class="{ active: currentTagIndex === index }"
            >{{ item.title }} ({{ item.tagCount }})</a
          >
        </div>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort" v-if="commentInfo">
      <span>排序：</span>
      <a
        href="javascript:;"
        @click="changeSort(null)"
        :class="{ active: reqParams.sortField === null }"
        >默认</a
      >
      <a
        href="javascript:;"
        @click="changeSort('createTime')"
        :class="{ active: reqParams.sortField === 'createTime' }"
        >最新</a
      >
      <a
        href="javascript:;"
        @click="changeSort('praiseCount')"
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        >最热</a
      >
    </div>
    <!-- 评价列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i
              v-for="i in 5 - item.score"
              :key="i"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attrs">{{ formatSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <!-- 图片预览组件 -->
          <goodsCommentImage
            v-if="item.pictures.length"
            :pictures="item.pictures"
          ></goodsCommentImage>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan">
              <i class="iconfont icon-dianzan"></i> {{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页 -->
    <xtx-pagination
      v-if="total"
      :total="total"
      v-model:current-page="reqParams.page"
      @current-change="reqParams.page = $event"
    ></xtx-pagination>
  </div>
  <div v-else class="loading"></div>
</template>

<script>
import { ref, inject, reactive, watch } from 'vue'
import goodsCommentImage from './goods-comment-image.vue'
import { useStore } from 'vuex'
import {
  findCommentInfoByGodods,
  findCommentListByGoods
} from '@/api/goods-detail'
export default {
  name: 'GoodsComment',
  components: { goodsCommentImage },
  setup() {
    // vuex
    const store = useStore()
    // 头部评价信息
    const goods = inject('goods')
    const commentInfo = ref(null)
    // 记录当前激活的标签标题
    const activeTitle = ref('全部评价')
    findCommentInfoByGodods(goods.value.id).then(({ result }) => {
      // 处理tags数组，方便模版v-for遍历
      result.tags = [
        {
          title: '全部评价',
          tagCount: result.evaluateCount
        },
        {
          title: '有图',
          tagCount: result.hasPictureCount
        },
        ...result.tags
      ]
      commentInfo.value = result
    })
    // 初始化需要发请求，筛选条件发生改变发请求【评价列表】
    // 准备筛选条件数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      // 排序方式：praiseCount 热度  createTime 最新
      sortField: null
    })
    const commentList = ref([])
    // 记录总条数
    const total = ref(0)
    watch(
      reqParams,
      () => {
        findCommentListByGoods(goods.value.id, reqParams).then((data) => {
          commentList.value = data.result.items
          total.value = data.result.counts
          store.commit('user/setCount', total)
        })
      },
      { immediate: true }
    )
    // 转换数据函数（对应vue2.0过滤器）
    const formatSpecs = (specs) => {
      return specs.map((v) => `${v.name}:${v.nameValue}`).join(' ')
    }
    const formatNickname = (nickname) => {
      return nickname.slice(0, 1) + '******' + nickname.slice(-1)
    }

    // 排序函数
    const changeSort = (type) => {
      reqParams.sortField = type
      reqParams.page = 1
    }
    // 激活tag
    const currentTagIndex = ref(0)
    const changeTag = (i) => {
      currentTagIndex.value = i
      // 设置有图和标签条件
      const currTag = commentInfo.value.tags[i]
      if (currTag === 'all') {
        reqParams.hasPicture = false
        reqParams.tag = null
      } else if (currTag === 'img') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = false
        reqParams.tag = currTag.title
      }
      reqParams.page = 1
    }
    return {
      commentInfo,
      activeTitle,
      reqParams,
      formatSpecs,
      formatNickname,
      commentList,
      currentTagIndex,
      changeSort,
      total,
      changeTag
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @color;
            background: lighten(@color, 50%);
            color: @color;
          }
          &.active {
            border-color: @color;
            background: @color;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @color;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        min-width: 25%;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
          flex-shrink: 0;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding: right 3px;
          }
          .attrs {
            padding-left: 10px;
            color: #666;
          }
        }
        .text {
          color: #666;
          line-height: 24px;
        }
        .time {
          color: #999;
          display: flex;
          justify-content: space-between;
          margin-top: 5px;
        }
      }
    }
  }
}
.loading {
  min-height: 600px;
  background-color: #fff !important;
  margin: 70px auto 45px auto;
  background: url(~@/assets/images/loading.gif) no-repeat center;
}
</style>
